import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

interface towDimCoor{
    x:number;
    y:number;
}
@ccclass('MG2')
export class MG2 extends Component {

    //平原的数量
    @property
    private plainCount: number = 0;
    // 地图的大小
    @property
    private mapSize: number = 15;
    //靠近地图中间的山地坐标
    private CenMountCoor:{x:number,y:number}={x:0,y:0};
    //
    Distdiff:{x:number,y:number}={x:this.mapSize / 2,y:this.mapSize / 2};

    // 自然地点及其生成概率和密度
    @property({ type: [String, Number, Number] })
    mapElements: { element: string, probability: number, density: number }[] = [
        { element: '平原', probability: 0.0001, density: 0.6 },
        { element: '森林', probability: 0.01, density: 0.01 },
        { element: '山地', probability: 0.02, density: 0.3 }
    ];

    //连贯的地点及其生成条数
    @property({ type: [String, Number] })
    mapConnections: { element: string, count: number }[] = [
        { element: '河流', count: 1 },
        { element: '道路', count: 0 }
    ];

    //特殊地点的生成参数
    @property({ type: [String, Number] })
    mapParticular: { element: string, count: number }[] = [
        { element: '村庄', count: 8 },
        { element: '小镇', count: 4 },
        { element: '城池', count: 2 }
    ];
    start() {
        // 生成随机地图
        const map = this.generateRandomMap();
        console.log('生成的地图:', map);
        // 输出平原的数量
        console.log('平原的数量:', this.plainCount);
        
        console.log('山地的位置:', this.CenMountCoor);
    }

    update(deltaTime: number) {

    }

    // 生成随机地图的方法
    generateRandomMap(): string[][] {
        const map: string[][] = [];
        for (let i = 0; i < this.mapSize; i++) {
            map[i] = [];
            for (let j = 0; j < this.mapSize; j++) {
                // 随机选择一个自然地点
                const randomElement = this.getRandomElement();
                // 将随机选择的自然地点添加到地图数组中
                map[i][j] = randomElement;
                if(randomElement==='山地'){
                    if((Math.abs(Math.floor(this.mapSize / 2)-i)+Math.abs(Math.floor(this.mapSize / 2)-j))<(this.Distdiff.x+this.Distdiff.y)){
                        this.CenMountCoor.x=i;
                        this.CenMountCoor.y=j;
                    this.Distdiff.x=Math.abs(Math.floor(this.mapSize / 2)-i);
                    this.Distdiff.y=Math.abs(Math.floor(this.mapSize / 2)-j);
                    }
                    // console.log('fff数量:', i+','+j);
                }
            }
        }
        this.generateConnections(map);
        return map;
    }

    // 根据概率和密度随机选择一个自然地点的方法
    getRandomElement(): string {
        let random = Math.random()*0.2;
        for (let element of this.mapElements) {
            if (random < element.probability * element.density) {
                if (element.element === '平原') {
                    this.plainCount++;
                }
                return element.element;
            }
            random -= element.probability * element.density;
        }
        // 如果没有匹配的概率，返回默认的自然地点
        return '    ';
    }

    // 生成连贯地点的方法
    generateConnections(map: string[][]) {
        for (let connection of this.mapConnections) {
            for (let i = 0; i < connection.count; i++) {
                // 随机选择一个起始点
                // let startX = Math.floor(Math.random() * this.mapSize);
                // let startY = Math.floor(Math.random() * this.mapSize);

                let startX = this.CenMountCoor.x;
                let startY = this.CenMountCoor.y;

                // 随机选择一个方向
                let direction = Math.floor(Math.random() * 8); // 0: 上, 1: 右, 2: 下, 3: 左

                // 生成连贯地点
                this.generateConnection(map, startX, startY, direction, connection.element);
            }
        }
    }

    // 生成单个连贯地点的方法
    generateConnection(map: string[][], x: number, y: number, direction: number, element: string) {
        map[x][y] = element;

        // 随机决定是否分支
        if (Math.random() < 0.3) { // 30% 的概率分支
            let newDirection = (direction + 0) % 8; // 转向
            this.generateConnection(map, x, y, newDirection, element);
        }

        // 继续延伸
        switch (direction) {
            case 0: // 上
                if (x > 0) this.generateConnection(map, x - 1, y, direction, element);
                break;
            case 1: // 右
                if (y < this.mapSize - 1) this.generateConnection(map, x, y + 1, direction, element);
                break;
            case 2: // 下
                if (x < this.mapSize - 1) this.generateConnection(map, x + 1, y, direction, element);
                break;
            case 3: // 左
                if (y > 0) this.generateConnection(map, x, y - 1, direction, element);
                break;
            case 4: // 上右
                if (x > 0&&y < this.mapSize - 1) this.generateConnection(map, x - 1, y+1, direction, element);
                break;
            case 5: // 下右
                if (x < this.mapSize - 1&&y < this.mapSize - 1) this.generateConnection(map, x+1, y + 1, direction, element);
                break;
            case 6: // 上左
                if (x > 0&&y > 0) this.generateConnection(map, x - 1, y-1, direction, element);
                break;
            case 7: // 下左
                if (x < this.mapSize - 1&&y < this.mapSize - 1) this.generateConnection(map, x+1, y + 1, direction, element);
                break;
        }
    }
}


